var wait_times=60;

$(document).ready(function () {

    //点击返回
    $(".go-back").click(function () {
        var url = $(this).attr("url");
        console.log(url);
        window.location = url;
    });

    //发送验证码
    $('#yzm').click(function () {

        if ($('#yzm').hasClass("disabled")) {
            return false;
        }

        var login_phone = $('#r_phone').val();
        if (login_phone == '') {
            layer.msg('手机号不能为空');
            return;
        }
        if (!isPhoneNumber(login_phone)) {
            layer.msg('手机号格式不正确');
            return;
        }
        $('#r_code').val('');

        $.post("/api/sms/send", { mobile: login_phone }, function (ret) {
            if (ret.code == 1) {
                layer.msg(ret.msg);
                settime(wait_times);
            } else {
                layer.msg(ret.msg);
            }
        }, 'json');

    })

    //点击注册
    $('.reg_btn').click(function () {
        var mobile = $('#r_phone').val();
        var r_code = $('#r_code').val();
        var password = $('#r_pass1').val();
        var password2 = $('#r_pass2').val();
        var share_code = $('#r_share_code').val();
        if (mobile == '') {
            layer.msg('手机号不能为空');
            return;
        }
        if (!isPhoneNumber(mobile)) {
            layer.msg('手机号格式不正确');
            return;
        }
        if (r_code == '') { layer.msg('短信验证码不能为空'); return; }
        if (password == '') { layer.msg('新密码不能为空'); return; }
        if (password2 == '') { layer.msg('确认密码不能为空'); return; }
        if (password != password2) { layer.msg('两次输入密码不一致'); return; }
        if (share_code == '') { layer.msg('分享码不能为空'); return; }
        $.post("/index/user/register", { mobile: mobile, captcha: r_code, password: password, password2: password2, share_code: share_code }, function (ret) {
            if (ret.code == 1) {
                layer.msg(ret.msg, { icon: 1, time: 1000 }, function () {
                    window.location.href = "/index/expert/index";
                });
            } else {
                layer.msg(ret.msg);
            }
        }, 'json');

    });

});

function settime(num) {
    var yzm_input = $("#yzm")
    var num;
    if (num == 0) {
        yzm_input.removeClass("disabled");
        yzm_input.html("重新发送");
        num = wait_times;
        return false;
    } else {
        yzm_input.addClass("disabled");
        yzm_input.html(num + "s后重发");
        num--;
    }
    setTimeout("settime(" + num + ")", 1000);
}
function isNumber(obj) {
    return !isNaN(parseFloat(obj)) && isFinite(obj);
}
function isPhoneNumber(tel) {
    var reg = /^0?1[3|4|5|6|7|8|9][0-9]\d{8}$/;
    return reg.test(tel);
}